home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2.sit / Raven 1.2 / Source / Foundation / Common / ZSimpleAllocator.h < prev    next >
Text File  |  1997-06-17  |  3KB  |  99 lines

  1. /*
  2.  *  File:       ZSimpleAllocator.h
  3.  *  Summary:    Fast general purpose allocator.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *    Abstract:    This uses the same algorithm MetroWerk's NEWMODE_FAST version of 
  7.  *                operator new. This is a fast and relatively simple algorithm but it 
  8.  *                does not return pools to the system. Note that TBestFitHeap is about 
  9.  *                as fast and does a much better job returning memory to the OS.
  10.  *
  11.  *  Copyright ゥ 1997 Jesse Jones. 
  12.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  13.  *
  14.  *  Change History (most recent first):
  15.  *
  16.  *         <->     1/28/97    JDJ        Created
  17.  */
  18.  
  19. #pragma once
  20.  
  21. #include <ZAllocator.h>
  22.  
  23.  
  24. //-----------------------------------
  25. //    Forward References
  26. //
  27. struct SBlock;
  28. struct SPool;
  29.  
  30.  
  31. // ===================================================================================
  32. //    class TSimpleAllocator
  33. // ===================================================================================
  34. class TSimpleAllocator : public TAllocator {
  35.  
  36.     typedef TAllocator Inherited;
  37.  
  38. //-----------------------------------
  39. //    Initialization/Destruction
  40. //
  41. public:
  42.     virtual                ~TSimpleAllocator();
  43.  
  44.                         TSimpleAllocator(ulong initialSize, ulong poolSize, ulong hugeSize = 0);
  45.                         // Heap will start with initialSize bytes. When the heap is 
  46.                         // exhausted a new pool with poolSize bytes will be allocated.
  47.                         // Blocks larger than hugeSize are always allocated using NewPtr 
  48.                         // and never reused (0 means poolSize/4).
  49.                         
  50. //-----------------------------------
  51. //    Inherited API
  52. //
  53. public:
  54.     virtual    void*        Allocate(ulong bytes);
  55.                         
  56.     virtual    void         Deallocate(void* block);
  57.  
  58.     virtual ulong         GetHeapSize() const                            {return mHeapSize;}
  59.             
  60.     virtual ulong         GetPoolCount() const                        {return mPoolCount;}
  61.                         
  62.     virtual ulong         GetBlockSize(const void* ptr) const;
  63.  
  64.     virtual    ulong         GetTotalBlockSize(const void* ptr) const;
  65.  
  66. #if DEBUG
  67.     virtual void         ValidateBlock(const void* ptr) const;
  68.             
  69.     virtual void         ValidateHeap(BlockValidateHook hook = nil, void* refCon = nil) const;
  70. #endif
  71.  
  72. //-----------------------------------
  73. //    Internal API
  74. //
  75. protected:
  76.             SBlock*     AllocateFromPool(SPool* pool, ulong bytes);
  77.  
  78.             SBlock*     AllocateFromBlock(SBlock* candidate, const SBlock* end, ulong bytes);
  79.                         
  80.             SBlock*     AllocateFromHeap(ulong bytes);
  81.  
  82.             SPool*         AllocatePool(ulong size);
  83.             
  84. //-----------------------------------
  85. //    Member Data
  86. //
  87. protected:    
  88.     SPool*            mFirstPool;
  89.     
  90.     ulong            mNewPoolSize;
  91.     ulong            mHugeSize;
  92.  
  93.     ulong            mHeapSize;        
  94.     ulong            mPoolCount;
  95.  
  96.     SBlock*            mLastSplitBlock;
  97.     const SBlock*    mLastEnd;
  98. };
  99.